class Solution {
    // 暴力枚举
    public int findPeakElement2(int[] nums) {
        // 找到严格大于左右元素即可
        int max = -1;
        for (int i = 0; i < nums.length; i++) {
            if (i-1 < 0) { // 只需判断右边的
                if (i+1 >= nums.length) {
                    max = 0; // 当只有一个元素时
                    break;
                } else {
                    if (nums[i] > nums[i+1]) {
                        max = i;
                        break;
                    }
                }
            } else { // 只需判断右边
                if (i+1 >= nums.length) {
                    // 判断左边
                    if (nums[i-1] < nums[i]) {
                        max = i;
                        break;
                    }
                } else {
                    // 判断两边
                    if (nums[i] > nums[i-1] && nums[i] > nums[i+1]) {
                        max = i;
                        break;
                    }
                }
            }
        }
        return max;
    }

    // 二分查找
    public int findPeakElement(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while (left < right) {
            int mid = left + (right-left) / 2;
            if (nums[mid] > nums[mid+1]) {
                right = mid;
            } else {
                left = mid+1;
            }
        }
        return left;
    }
}